using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using System.Text;
using System.Xml.Serialization;
using System.Runtime.Serialization;

namespace TicTacToe
{
    [DataContract]
    public enum TicTacToePlayer { [EnumMember(Value = "None")]None, [EnumMember(Value = "PlayerO")] PlayerO, [EnumMember(Value = "PlayerX")] PlayerX }
    public enum BoardSize { Three_x_Three = 3, Six_x_Six = 6 }
    public enum GameScreen { First, Single, Multy }

    public enum Style_Texture { Classic, Modern }
    
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class GameMain : Microsoft.Xna.Framework.Game
    {
        GameScreen gameScreen;       
        
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;          

        int backBufferedWidth;
        int backBufferedHeight;

        bool touching;

        GamePlay gamePlay;

        public GameMain()
        {
            gameScreen = GameScreen.Single;
            touching = false;

            backBufferedWidth = 480;
            backBufferedHeight = 800;

            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            graphics.PreferredBackBufferWidth = backBufferedWidth;
            graphics.PreferredBackBufferHeight = backBufferedHeight;

            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks(333333);

            if (gameScreen == GameScreen.Single)
            {
                gamePlay = new SinglePlay(this, backBufferedWidth, backBufferedHeight);
            }
            else if (gameScreen == GameScreen.Multy)
            {
                gamePlay = new MultiPlay(this, backBufferedWidth, backBufferedHeight);
            }
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            gamePlay.Initialize();
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            if (gameScreen == GameScreen.First)
            {
            }
            else if (gameScreen == GameScreen.Single)
            {
                gamePlay.LoadContent();
            }
            else if (gameScreen == GameScreen.Multy)
            {
            }
            

            spriteBatch = new SpriteBatch(GraphicsDevice);
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        // Run periodcally
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || 
                GamePad.GetState(PlayerIndex.One).Buttons.Start == ButtonState.Pressed)
            {
                this.Exit();
            }

            TouchCollection touches = TouchPanel.GetState();

            if (!touching && touches.Count > 0)
            {
                touching = true;
                TouchLocation touch = touches.First();

                gamePlay.HandleTouch(touch);
            }
            else if (touches.Count == 0)
            {
                touching = false;
            }

            base.Update(gameTime);
        }

        // Run periodcally
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            gamePlay.Draw();
            base.Draw(gameTime);
        }
    }
}
